<?php
namespace app\admin\controller;
use think\facade\Db;
use PhpOffice\PhpSpreadsheet\IOFactory;

class industry  {
    public function index() {
        $reader = IOFactory::createReader('Xls');
        $spreadsheet = $reader->load(public_path().'static/gmjj2017.xls');
        // 获取活动工作簿
        $sheet = $spreadsheet->getActiveSheet();

        // 获取内容的最大列 如 D
        $highest = $sheet->getHighestColumn();

        // 获取内容的最大行 如 4
        $row = $sheet->getHighestRow();

        $data = [];
        $pcode = 0;
        $x=0;
        for ($i = 1; $i <= $row; $i++) {
            $t1 = $sheet->getCellByColumnAndRow(1, $i)->getValue();
            $t2 = $sheet->getCellByColumnAndRow(2, $i)->getValue();
            $t3 = $sheet->getCellByColumnAndRow(3, $i)->getValue();
            $t4 = $sheet->getCellByColumnAndRow(4, $i)->getValue();
            $t5 = $sheet->getCellByColumnAndRow(5, $i)->getValue();
            $t6 = $sheet->getCellByColumnAndRow(6, $i)->getValue();

            // if(isset($t1)&&$t1!=' ') {
            //     var_dump($t1);
            //     print_r('----<br/>');
            //     $value = array(
            //         'name' =>   $t5,
            //         'code' =>   $t1,
            //         'level'=>1,
            //         'year'=>2017
            //         //'parent_code' => $pcode,
            //     );

            //     if(isset($t6)) {
            //         $value['memo'] = $t6;
            //     }         
            //     Db::table('yhq_industry')->insert($value);
            // }

            // if(isset($t1)&&$t1!=' ') {
            //     $pcode = $t1;
            // }

            // if($t1==' '&&isset($t2)&&$t2!=' ') {
            //     print_r($t2);
            //     print_r('<br/>');
            //     $value = array(
            //         'name' =>   $t5,
            //         'code' =>   $t2,
            //         'level'=>2,
            //         'year'=>2017,
            //         'parent_code' => $pcode,
            //     );

            //     if(isset($t6)) {
            //         $value['memo'] = $t6;
            //     }                
            //     Db::table('yhq_industry')->insert($value);
            // }

            // if($t1==' '&&isset($t2)&&$t2!=' ') {
            //     $pcode = $t2;
            // }

            // if(($t1==' '&&$t2==' '&&isset($t3)&&$t3!=' ')||($t1==' '&&$t2!=' '&&isset($t3)&&$t3!=' ')||($t1!=' '&&$t2!=' '&&isset($t3)&&$t3!=' ')) {
            //     print_r($t3);
            //     print_r('<br/>');
            //     $value = array(
            //         'name' =>   $t5,
            //         'code' =>   $t3,
            //         'level'=>3,
            //         'year'=>2017,
            //         'parent_code' => $pcode,
            //     );

            //     if(isset($t6)) {
            //         $value['memo'] = $t6;
            //     }                
            //     Db::table('yhq_industry')->insert($value);
            // }



            if(isset($t3)&&$t3!=' ') {
                $pcode = $t3;
            }
            
            if($t4!=' ') {
                $x++;
                print_r($t4);
                print_r('<br/>');
                $value = array(
                    'name' =>   $t5,
                    'code' =>   $t4,
                    'level'=>4,
                    'year'=>2017,
                    'parent_code' => $pcode,
                );

                if(isset($t6)) {
                    $value['memo'] = $t6;
                }                
                // Db::table('yhq_industry')->insert($value);
            }
        }
        print_r($x);
    }

    public function getList() {
        $where = [];
        if(isset($_GET['pcode'])) {
            $where['parent_code'] = $_GET['pcode'];
        } else {
            $where['parent_code'] = null;
        }
        if(isset($_GET['year'])) {
            $where['year'] = $_GET['year'];
        } else {
            $where['year'] = 2017;
        }
        $list = Db::table('yhq_industry')->where($where)->select();
        return array('list'=>$list);
    }
}